package com.mju.config;

import com.mju.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

// 拦截器注册
@Configuration
public class LoginInterceptorConfigure implements WebMvcConfigurer {
    //// 配置url映射本地图片路径
    //@Value("${file.upload.location}")
    //private String fileLocation;
    //@Value("${file.upload.urlPath}")
    //private String fileUrlPath;
    //@Override
    //public void addResourceHandlers(ResourceHandlerRegistry registry) {
    //    // 获取项目路径
    //    String projectPath = System.getProperty("user.dir");
    //    // 拼接最终路径
    //    String finalFileLocation = "file:" + projectPath + fileLocation;
    //    System.out.println("最终：" + finalFileLocation);
    //    System.out.println("fileUrlPath:" + fileUrlPath);
    //    registry.addResourceHandler(fileUrlPath) // 配置要要映射的url路径，一般自定义即可
    //            .addResourceLocations(finalFileLocation); // 配置图片的本地路径
    //}

    // 配置拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 1. 创建登陆拦截器对象
        HandlerInterceptor interceptor = new LoginInterceptor();
        // 2. 配置白名单(不进行session验证)
        List<String> patterns = new ArrayList<>();
        patterns.add("/bootstrap3/**");
        patterns.add("/css/**");
        patterns.add("/images/**");
        patterns.add("/js/**");
        patterns.add("/web/register.html");
        patterns.add("/web/login.html");
        patterns.add("/web/index.html");
        patterns.add("/web/product.html");
        patterns.add("/users/reg");
        patterns.add("/users/login");
        patterns.add("/districts/**"); // 确保不登录，也能访问该路径。
        patterns.add("/products/**");
        // 拦截
        registry.addInterceptor(interceptor)
                .addPathPatterns("/**") // 拦截所有路径
                .excludePathPatterns(patterns); // 排除白名单的数据

    }
}
